﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using VNet.VisiSuite.Service.Contact.Enum;

namespace VNet.VisiSuite.Common.Helper
{
    /// <summary>
    /// Image helper class
    /// </summary>
    public static class ImageHelper
    {
        /// <summary>
        /// Converts to list byte array.
        /// </summary>
        /// <param name="images">The images.</param>
        /// <returns></returns>
        public static List<byte[]> ConvertToListByteArray(List<Image> images)
        {
            return images.Select(ConvertImageToByteArray).ToList();
        }

        /// <summary>
        /// Converts the image to byte array.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <returns></returns>
        public static byte[] ConvertImageToByteArray(Image image)
        {
            var stream = new MemoryStream();
            image.Save(stream, ImageFormat.Png);
            return stream.ToArray();
        }

        /// <summary>
        /// Converts to images.
        /// </summary>
        /// <param name="imageBytes">The image bytes.</param>
        /// <returns></returns>
        public static IList<Image> ConvertToImages(List<byte[]> imageBytes)
        {
            return imageBytes.Select(ConvertByteArrayToImage).ToList();
        }

        /// <summary>
        /// Converts the by array to image.
        /// </summary>
        /// <param name="imageByte">The image byte.</param>
        /// <returns></returns>
        public static Image ConvertByteArrayToImage(byte[] imageByte)
        {
            if (imageByte == null) return null;

            byte[] buffer = imageByte.ToArray();
            var stream = new MemoryStream();
            stream.Write(buffer, 0, buffer.Length);
            return Image.FromStream(stream);
        }

        /// <summary>
        /// Gets the image by URL.
        /// </summary>
        /// <param name="imagePath">The image path.</param>
        /// <returns></returns>
        public static byte[] GetImageByUrl(string imagePath, ImageType imageType)
        {
            if (string.IsNullOrEmpty(imagePath))
                return null;

            string path = string.Empty;
            //If this method is called from server, imagePath is only the filename.
            if (imagePath.LastIndexOf('\\') == -1)
                path = string.Format("{0}{1}", FileHelper.BuildFolderPath(imageType), imagePath);
            else //if this method is called from client, imagePath is the full path of the file.
                path = imagePath;
            try
            {
                var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
                var imageDataRaw = new byte[fileStream.Length];
                fileStream.Read(imageDataRaw, 0, (int)fileStream.Length);
                fileStream.Close();
                return imageDataRaw;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Gets the image by URL.
        /// </summary>
        /// <param name="imagePath">The image path.</param>
        /// <returns></returns>
        public static byte[] GetLogoImageByUrl(string imagePath)
        {
            if (string.IsNullOrEmpty(imagePath))
                return null;

            try
            {
                var fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
                var imageDataRaw = new byte[fileStream.Length];
                fileStream.Read(imageDataRaw, 0, (int)fileStream.Length);
                fileStream.Close();
                return imageDataRaw;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}